package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillOceanApplyBiz;
import com.egao.orderB.entity.BillOceanApply;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-09-14 14:07:15
 * @version ：1.0
 */
@Repository
public class BillOceanApplyBizImpl extends BaseDao implements BillOceanApplyBiz {

	@Override
	public JsonResult list(PageResult2<T> page, BillOceanApply billOceanApply, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillOceanApply> rm = new BeanPropertyRowMapper<BillOceanApply>(BillOceanApply.class);
		StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.bill_id");
    sql.append(",b.ocean_id,b.apply_id,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_bill_ocean_apply b ");
    sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id and sogzn.deleted=0 ");
    sql.append(" where b.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and b.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(billOceanApply)){
//			if(Utils.isNotEmpty(billOceanApply.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", billOceanApply.getName());
//			}
      if(Utils.isNotEmpty(billOceanApply.getSort())&&Utils.isNotEmpty(billOceanApply.getOrder())){
        sql.append(" order by b."+billOceanApply.getSort()+" "+billOceanApply.getOrder());
      } else {
        sql.append(" order by b.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<BillOceanApply> list(BillOceanApply billOceanApply, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillOceanApply> rm = new BeanPropertyRowMapper<BillOceanApply>(BillOceanApply.class);
    StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.bill_id");
    sql.append(",b.ocean_id,b.apply_id,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_bill_ocean_apply b ");
    sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id and sogzn.deleted=0 ");
    sql.append(" where b.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and b.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(billOceanApply)){
      if(Utils.isNotEmpty(billOceanApply.getSort())&&Utils.isNotEmpty(billOceanApply.getOrder())){
        sql.append(" order by b."+billOceanApply.getSort()+" "+billOceanApply.getOrder());
      } else {
        sql.append(" order by b.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public List<BillOceanApply> setTriAngle(BillOceanApply billOceanApply) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillOceanApply> rm = new BeanPropertyRowMapper<BillOceanApply>(BillOceanApply.class);
    StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.bill_id");
    sql.append(",b.ocean_id,b.apply_id,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_bill_ocean_apply b ");
    sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id and sogzn.deleted=0 ");
    sql.append(" where b.deleted=0 ");
    if(Utils.isNotEmpty(billOceanApply)){
      if(Utils.isNotEmpty(billOceanApply.getSort())&&Utils.isNotEmpty(billOceanApply.getOrder())){
        sql.append(" order by b."+billOceanApply.getSort()+" "+billOceanApply.getOrder());
      } else {
        sql.append(" order by b.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public BillOceanApply getTriangleById(BillOceanApply billOceanApply) {
		RowMapper<BillOceanApply> rm = new BeanPropertyRowMapper<BillOceanApply>(BillOceanApply.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.bill_id");
    sql.append(",b.ocean_id,b.apply_id,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
    sql.append(" from chf_bill_ocean_apply b ");
    sql.append(" where b.deleted=0 ");
		if(Utils.isNotEmpty(billOceanApply)){
			if(Utils.isNotEmpty(billOceanApply.getBillId())){
				sql.append(" and b.bill_id=:bill_id ");
				map.put("bill_id", billOceanApply.getBillId()+"");
			}
			if(Utils.isNotEmpty(billOceanApply.getOceanId())){
				sql.append(" and b.ocean_id=:ocean_id ");
				map.put("ocean_id", billOceanApply.getOceanId()+"");
			}
			if(Utils.isNotEmpty(billOceanApply.getApplyId())){
				sql.append(" and b.apply_id=:apply_id ");
				map.put("apply_id", billOceanApply.getApplyId()+"");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public BillOceanApply getById(BillOceanApply billOceanApply) {
		RowMapper<BillOceanApply> rm = new BeanPropertyRowMapper<BillOceanApply>(BillOceanApply.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.bill_id");
    sql.append(",b.ocean_id,b.apply_id,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_bill_ocean_apply b ");
    sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id and sogzn.deleted=0 ");
    sql.append(" where b.deleted=0 and b.id=:id ");
		map.put("id", billOceanApply.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(BillOceanApply billOceanApply, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_bill_ocean_apply (");
    sql.append("organization_id,bill_id,ocean_id,apply_id");
		sql.append(",create_user,create_time) values ( ");
    sql.append(":organization_id,:bill_id,:ocean_id,:apply_id");
		sql.append(",:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("bill_id", billOceanApply.getBillId()==null?null:billOceanApply.getBillId()+"");
    map.put("ocean_id", billOceanApply.getOceanId()==null?null:billOceanApply.getOceanId()+"");
    map.put("apply_id", billOceanApply.getApplyId()==null?null:billOceanApply.getApplyId()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(BillOceanApply billOceanApply, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_bill_ocean_apply set ");
    sql.append("bill_id=:bill_id,ocean_id=:ocean_id,apply_id=:apply_id ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("bill_id", billOceanApply.getBillId()==null?null:billOceanApply.getBillId()+"");
    map.put("ocean_id", billOceanApply.getOceanId()==null?null:billOceanApply.getOceanId()+"");
    map.put("apply_id", billOceanApply.getApplyId()==null?null:billOceanApply.getApplyId()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", billOceanApply.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(BillOceanApply billOceanApply, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(billOceanApply.getId().length()>0){
			for(String ID : billOceanApply.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_bill_ocean_apply set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
